package com.example.capsule_chat0322.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.example.capsule_chat0322.R;


/**
 * @Author zhuzhiming
 * @Date 2022/5/5 15:28
 */
public class AvatarImagesActivity extends Activity {

    private ImageView[][] iv = new ImageView[3][10];

    //    int[] ids = { R.id.image_01, R.id.image_02, R.id.image_03, R.id.image_04,
    //            R.id.image_05, R.id.image_06, R.id.image_07, R.id.image_08,
    //            R.id.image_09 };
    //    int[] imgId = { R.drawable.img01, R.drawable.img02, R.drawable.img03,
    //            R.drawable.img04, R.drawable.img05, R.drawable.img06,
    //            R.drawable.img07, R.drawable.img08, R.drawable.img09 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avatar_images);

        for (int k = 0; k < iv.length; k++) {
            //使用反射
            for (int i = 0; i < iv[0].length; i++) {

                final int finalI = i + 1;
                String name = "image_" + k + finalI;

                try {
                    //获取其他类
                    Class cls = R.id.class;
                    //获取类的属性,getField(name)获取属性，getInt(null)获取属性对应的值
                    //null代表的是静态变量，非静态变量需要传递对象
                    int id = cls.getField(name).getInt(null);
                    iv[k][i] = (ImageView) findViewById(id);
                    int finalK = k;
                    iv[k][i].setOnClickListener(v -> {

                        //返回数据
                        Intent intent = getIntent();
                        Class cls2 = R.drawable.class;
                        try {
                            //内部类使用外部局部变量，需要final
                            int imgId2 = cls2.getField("img_" + finalK + finalI).getInt(null);
                            intent.putExtra("image", imgId2);
                            setResult(RESULT_OK, intent);
                            finish();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


        Class cls = R.id.class;
        int id = 0;
        try {
            id = cls.getField("image_10").getInt(null);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        iv[1][0] = (ImageView) findViewById(id);
        iv[1][0].setOnClickListener(v -> {
            Intent intent = getIntent();
            Class cls2 = R.drawable.class;
            try {
                int imgId2 = cls2.getField("img_10").getInt(null);
                intent.putExtra("image", imgId2);
                setResult(RESULT_OK, intent);
                finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });


        //        for (int i = 0; i < iv.length; i++) {
        //            final int finalI = i;
        //            //给每一个ImageView找到id
        //            iv[i] = (ImageView) findViewById(ids[i]);
        //            //设置点击事件监听
        //            iv[i].setOnClickListener(new OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    //返回数据
        //                    Intent intent = getIntent();
        //                    //内部类使用外部局部变量，需要final
        //                    intent.putExtra("image", imgId[finalI]);
        //                    setResult(RESULT_OK, intent);
        //                    finish();
        //                }
        //            });
        //        }
    }


}
